package com.zryc.shop.ums.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.base.Result;
import com.zryc.shop.ums.entity.Admin;
import com.zryc.shop.ums.entity.Menu;
import com.zryc.shop.ums.entity.Menu;
import com.zryc.shop.ums.entity.Role;
import com.zryc.shop.ums.mapper.MenuMapper;
import com.zryc.shop.ums.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zryc.shop.ums.service.IRoleService;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 后台菜单表 服务实现类
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-16
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {


    /**
     * 更新菜单信息
     *
     * @param id
     * @param menu
     * @return
     */
    @Override
    public Object updateMenu(Long id, Menu menu) {
        UpdateWrapper<Menu> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        baseMapper.update(menu,updateWrapper);
        return null;
    }

    /**
     * 分页查询menu
     *
     * @param parentId 关键字
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<Menu> list(Integer parentId, Integer pageNum, Integer pageSize) {
        Page<Menu> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        return page(page, wrapper);
    }

    /**
     * 根据角色id 查询菜单列表
     *
     * @param roleId
     * @return
     */
    @Override
    public List<Menu> getMenuByRoleId(Long roleId) {
        List<Menu> menus = this.getBaseMapper().getMenuByRoleId(roleId);
        return menus;
    }

    /**
     * 更新菜单状态
     *
     * @param id
     * @param hidden
     * @return
     */
    @Override
    public Object updateMenuHidden(Long id, Integer hidden) {
        UpdateWrapper<Menu> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("hidden", hidden);
        update(updateWrapper);
        return null;
    }

    /**
     * 删除中间表和菜单表该菜单和子菜单记录
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Object deleteMenuById(Long id) {
        ArrayList<Long> ids = new ArrayList<>();
        ids.add(id);
        this.getIds(ids,id);//获取子菜单。。。的ID集合
        baseMapper.deleteBatchIds(ids);//删除菜单表中记录
        if (CollectionUtils.isNotEmpty(ids)) {
            for (Long menuId : ids) {
                baseMapper.deleteFromMiddle(menuId);//删除中间表中的菜单记录
            }
        }
        return "删除成功";
    }

    /**
     * 获取子id
     * @param ids
     * @param id
     */
    private void getIds(List<Long> ids, Long id) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Menu> menus = baseMapper.selectList(wrapper);
        if(CollectionUtils.isNotEmpty(menus)){
            for(Menu menu:menus){
                //获取要删除的id
                Long i=menu.getId();
                ids.add(i);
                //递归查找该id下的子评论
                this.getIds(ids,i);
            }
        }
    }

    /**
     * 给菜单建树
     *
     * @param menuList
     * @return
     */
    @Override
    public Object buildtree(List<Menu> menuList) {
        // 1. 获取根菜单
        List<Menu> rootMenuList=new ArrayList<>();
        for(Menu menu:menuList){
            if(menu.getParentId()==0){
                rootMenuList.add(menu);
            }
        }
        // 2. 跟菜单下的子菜单
        for(Menu menu:rootMenuList){
            childrenMenu(menuList,menu);
        }
        return rootMenuList;
    }
    private Menu childrenMenu(List<Menu> menuList,Menu menu){
        //存放menu菜单对象的所有子菜单
        List<Menu> children = new ArrayList<>();
        for(Menu m:menuList){
            //判断每一个菜单的parent_id是否等于menu对象中的id
            if (m.getParentId().equals(menu.getId())) {
                children.add(childrenMenu(menuList,m));
            }
        }
        menu.setChildren(children);
        return menu;
    }


}
